package main

import (
	"github.com/kataras/iris/v12"
)
const maxSize = 8 * iris.MB

func main() {
	app := iris.Default()
	// 单个文件上传
	app.Post("/upload", func(ctx iris.Context) {
		// Set a lower memory limit for multipart forms (default is 32 MiB)
		ctx.SetMaxRequestBodySize(maxSize)
		// OR
		// app.Use(iris.LimitRequestBodySize(maxSize))
		// OR
		// OR iris.WithPostMaxMemory(maxSize)

		// single file
		_, fileHeader, err:= ctx.FormFile("file")
		// 多文件上传
		// files, n, err := ctx.UploadFormFiles("./uploads")
		if err != nil {
			ctx.StopWithError(iris.StatusBadRequest, err)
			return
		}

		// 上传文件到确定的位置
		dest := "./uploadFiles/" + fileHeader.Filename
		ctx.SaveFormFile(fileHeader, dest)

		ctx.Writef("File: %s uploaded!", fileHeader.Filename)
	})

	app.Listen(":8080")
}
